package cn.iocoder.yudao.module.picturebook.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.user.PBUserDO;
import cn.iocoder.yudao.module.picturebook.service.user.PBUserService;

@Tag(name = "绘本系统 - 用户信息")
@RestController
@RequestMapping("/picturebook/PB-user")
@Validated
public class PBUserController {

    @Resource
    private PBUserService pBUserService;

    @PostMapping("/create")
    @Operation(summary = "创建用户信息")
    @PreAuthorize("@ss.hasPermission('picturebook:PB-user:create')")
    public CommonResult<Long> createPBUser(@Valid @RequestBody PBUserSaveReqVO createReqVO) {
        return success(pBUserService.createPBUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    @PreAuthorize("@ss.hasPermission('picturebook:PB-user:update')")
    public CommonResult<Boolean> updatePBUser(@Valid @RequestBody PBUserSaveReqVO updateReqVO) {
        pBUserService.updatePBUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:PB-user:delete')")
    public CommonResult<Boolean> deletePBUser(@RequestParam("id") Long id) {
        pBUserService.deletePBUser(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户信息")
                @PreAuthorize("@ss.hasPermission('picturebook:PB-user:delete')")
    public CommonResult<Boolean> deletePBUserList(@RequestParam("ids") List<Long> ids) {
        pBUserService.deletePBUserListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:PB-user:query')")
    public CommonResult<PBUserRespVO> getPBUser(@RequestParam("id") Long id) {
        PBUserDO pBUser = pBUserService.getPBUser(id);
        return success(BeanUtils.toBean(pBUser, PBUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户信息分页")
    @PreAuthorize("@ss.hasPermission('picturebook:PB-user:query')")
    public CommonResult<PageResult<PBUserRespVO>> getPBUserPage(@Valid PBUserPageReqVO pageReqVO) {
        PageResult<PBUserDO> pageResult = pBUserService.getPBUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PBUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户信息 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:PB-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPBUserExcel(@Valid PBUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PBUserDO> list = pBUserService.getPBUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户信息.xls", "数据", PBUserRespVO.class,
                        BeanUtils.toBean(list, PBUserRespVO.class));
    }

}